package com.party.service.study.impl;

import com.party.common.Page;
import com.party.mapper.AnswerMapper;
import com.party.model.Answer;
import com.party.model.AnswerExample;
import com.party.model.Test;
import com.party.service.study.AnswerService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class AnswerServiceImpl implements AnswerService
{
    @Resource
    AnswerMapper answerMapper;

    @Override
    public int add(Answer answer) {
        if(answer != null)
        {
            return this.answerMapper.insert(answer);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if(id != null && id > 0)
        {
            return this.answerMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public Answer getById(Integer id) {
        if(id != null && id > 0)
        {
            return this.answerMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public List<Answer> getByIds(List<Integer> ids) {
        if(ids != null && ids.size() > 0)
        {
            AnswerExample example = new AnswerExample();
            AnswerExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(ids);
            return this.answerMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<Answer> getByTestId(Integer id) {
        if(id != null && id > 0)
        {
            AnswerExample example = new AnswerExample();
            AnswerExample.Criteria criteria = example.createCriteria();
            criteria.andTestidEqualTo(id);
            example.setOrderByClause("id desc");
            return this.answerMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public Page getList(Answer answer, int pageSize, int pageNumber) {

        AnswerExample example = new AnswerExample();
        if(answer != null)
        {
            AnswerExample.Criteria criteria = example.createCriteria();
            if(answer.getContent() != null && !"".equals(answer.getContent()))
            {
                criteria.andContentLike("%"+answer.getContent()+"%");
            }
        }
        //获取总的记录数量
        int count = this.answerMapper.countByExample(example);
        //获取指定的页码数据
        example.setBeginNum((pageNumber - 1) * pageSize);
        example.setEndNum(pageSize);
        List<Test> list = this.answerMapper.getListPage(example);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;

    }
}
